package com.hontech.libratest.activity.fragment.sub

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.hontech.libratest.R
import com.hontech.libratest.activity.window.showProg
import com.hontech.libratest.conn.getClaspSize
import com.hontech.libratest.conn.setClaspSize
import com.hontech.libratest.utils.onClick
import com.hontech.libratest.utils.toInt

class ClaspSub (view: View) {

    private val etRow = view.findViewById<EditText>(R.id.id_main_et_clasp_row)
    private val etSize = view.findViewById<EditText>(R.id.id_main_et_clasp_size)
    private val btnSet = view.findViewById<Button>(R.id.id_main_btn_set_clasp)
    private val btnGet = view.findViewById<Button>(R.id.id_main_btn_get_clasp)

    init {
        btnSet.onClick(::onClickSet)
        btnGet.onClick(::onClickGet)
    }

    private suspend fun onClickSet() {
        val w = showProg(btnSet, "设置挂钩步距")
        try {
            val row = etRow.toInt()
            val size = etSize.toInt()
            setClaspSize(row, size)
            w.success("设置成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickGet() {
        val w = showProg(btnSet, "获取挂钩步距")
        try {
            val row = etRow.toInt()
            val size = getClaspSize(row)
            w.success("成功 第${row}行步距:${size}")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }
}